/*
https://leetcode-cn.com/problems/sort-colors/solution/yan-se-fen-lei-by-leetcode-solution/
 */
import java.util.Arrays;

public class Solution75 {
    public void sortColors(int[] nums) {
        int p0=0,p1=0;
        for (int i=0;i<nums.length;i++){
            if (nums[i]==1){
                nums[i]=nums[p1];
                nums[p1]=1;
                p1++;
            }
            if (nums[i]==0){
                nums[i]=nums[p0];
                nums[p0]=0;
                if (p0<p1){
                    nums[i]=nums[p1];
                    nums[p1]=1;
                }
                p1++;
                p0++;
            }
        }
    }

    public static void main(String[] args) {
        int[] nums=new int[]{2,0,2,1,1,0};
        new Solution75().sortColors(nums);
        System.out.println(Arrays.toString(nums));
    }
}
